package main.od;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class DiseaseSpread {

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                String input = sc.nextLine();
                String[] inputArr = input.split(",");

                int arrCount = inputArr.length;
                Set<Integer> diseaseSet = new HashSet<Integer>();
                for (int i = 0; i < arrCount; i++) {
                    int value = Integer.parseInt(inputArr[i]);
                    if (value == 1) {
                        diseaseSet.add(i);
                    }
                }
                processDiseaseSpread(arrCount, diseaseSet);

            }

        }

        private static void processDiseaseSpread(int arrCount, Set<Integer> diseaseSet) {
            if (diseaseSet.size() == 0 || diseaseSet.size() == arrCount) {
                System.out.println(-1);
                return;
            }
            int days = 0;
            // 避免误差，使用 Math.round。
            int dimension = (int) Math.round(Math.sqrt(arrCount));

            Set<Integer> newDiseaseSet = new HashSet<Integer>();
            newDiseaseSet.addAll(diseaseSet);

            while (diseaseSet.size() != arrCount) {
                for (Iterator<Integer> iter = diseaseSet.iterator(); iter.hasNext();) {
                    int coords = iter.next();
                    updateNewCoords(dimension, coords, newDiseaseSet);
                }
                days++;
                if (newDiseaseSet.size() == arrCount) {
                    System.out.println(days);
                    return;
                }
                diseaseSet = newDiseaseSet;
            }

        }

        private static void updateNewCoords(int dimension, int coords, Set<Integer> newDiseaseSet) {
            int i = coords / dimension;
            int j = coords % dimension;

            int[][] fourDirections = { { i, j - 1 }, { i, j + 1 }, { i - 1, j }, { i + 1, j } };
            for (int k = 0; k < fourDirections.length; k++) {
                int curI = fourDirections[k][0];
                int curJ = fourDirections[k][1];
                if (curI < 0 || curJ < 0 || curI >= dimension || curJ >= dimension) {
                    continue;
                }
                Integer curCoords = curI * dimension + curJ;
                if (!newDiseaseSet.contains(curCoords)) {
                    newDiseaseSet.add(curCoords);
                }
            }

        }

}
